package oaSystem.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import oaSystem.biz.EmployeeBiz;
import oaSystem.entity.Employee;
import oaSystem.entity.EmployeeRole;
import oaSystem.entity.Function;
import oaSystem.entity.Role;
import oaSystem.mapper.EmployeeMapper;
import oaSystem.mapper.EmployeeRoleMapper;
import oaSystem.mapper.FunctionMapper;
import oaSystem.mapper.RoleMapper;
@Service
public class EmployeeBizImpl implements EmployeeBiz{
	@Autowired
	private EmployeeMapper employeeMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private FunctionMapper functionMapper;
	@Autowired
	private EmployeeRoleMapper employeeRoleMapper;
	@Override
	public Employee fetchEmployeeById(int id) {
		Employee employee = employeeMapper.fetchEmployeeById(id);
		List<Role> roleList = roleMapper.findRoleByEmployeeId(employee.getId());
		employee.setRoles(roleList);
		return employee;
	}

	@Override
	public List<Employee> getEmployeeByDepartmentIdAndSearch(Integer departmentId, int pageNum, int pageSize,
			String search,String nrsearch) {
		return employeeMapper.getEmployeeByDepartmentIdAndSearch(departmentId, 
																	(pageNum-1)*pageSize, 
																	pageSize, 
																	search,
																	nrsearch);
	}
	
	@Override
	public List<Employee> findEmployeeByDepartmentId(Integer departmentId){
		return employeeMapper.findEmployeeByDepartmentId(departmentId);
	}
	
	@Override
	public Employee checkLogin(String nr,String password){
		Employee employee = employeeMapper.checkLogin(nr, password);
		List<Role> roleList = roleMapper.findRoleByEmployeeId(employee.getId());
		employee.setRoles(roleList);
		if(roleList!=null&&roleList.size()>0){
			for (Role role : roleList) {
				Map<String,List<Function>> map = role.getFunctions();
				List<Function> functionList = functionMapper.findFunctionByRoleId(role.getId());
				//一个角色的所有功能
				role.setFunctionAll(functionList);
				List<Function> menuItem = getMenuItemFuction(functionList,true);
				map.put("menuItem", menuItem);
				List<Function> pageMenuItem = getMenuItemFuction(functionList,false);
				map.put("pageMenuItem", pageMenuItem);
			}
		}
		return employee;
	}
	
	/**
	 * 获取菜单栏的功能
	 * @param functionList
	 * @return
	 */
	private List<Function> getMenuItemFuction(List<Function> functionList,boolean isMenuItem) {
		List<Function> list = new ArrayList<Function>();
		for (Function function : functionList) {
			if(function.getParentId()==null){
				list.add(function);
				getChildrenItemOfMenuItem(function.getId(),function,functionList,isMenuItem);
			}
		}
		return list;
	}
	/**
	 * 获取菜单栏的子项
	 * @param id
	 * @param functionList
	 */
	private void getChildrenItemOfMenuItem(Integer id, Function parent,List<Function> functionList,boolean isMenuItem) {
		List<Function> children=null;
		
		for (Function function : functionList) {
			if(isMenuItem){//获取菜单栏子项
				children = parent.getChildren();
				if(function.getParentId()!=null&&function.getParentId()==id&&function.getIsMenultem()==true){
					children.add(function);
					getChildrenItemOfMenuItem(function.getId(),function,functionList,isMenuItem);
				}
			}else{//获取具体页面的子项
				children = parent.getPageChildren();
				if(function.getParentId()!=null&&function.getParentId()==id&&function.getIsMenultem()==false){
					children.add(function);
					getChildrenItemOfMenuItem(function.getId(),function,functionList,isMenuItem);
				}
			}
			
		}
	}
	

	
	/**
	 * 添加员工
	 */
	@Transactional(propagation=Propagation.REQUIRED, timeout=60)
	@Override
	public void addEmployee(Employee employee) {
		//添加到Employee表
		employeeMapper.addEmployee(employee);
		
		//给员工添加角色(employeeRole)
		List<Role> roles = employee.getRoles();
		if(roles!=null&&roles.size()>0){
			for (Role role : roles) {
				EmployeeRole emRole=new EmployeeRole(null, employee, role);
				employeeRoleMapper.addEmployeeRole(emRole);
			}
		}
	}
	@Transactional(propagation=Propagation.REQUIRED, timeout=60)
	@Override
	public void updateEmployee(Employee employee) {
		//更新员工表信息
		employeeMapper.updateEmployee(employee);
		//获取employee原本的角色(employeeRole表)
		List<EmployeeRole> erList = employeeRoleMapper.findEmployeeRoleByEmpId(employee.getId());
		if(erList!=null && erList.size()>0){
			//把原本的角色删除
			for (EmployeeRole employeeRole : erList) {
				employeeRoleMapper.deleteEmployeeRole(employeeRole.getId());
			}
		}
		
		//把新的角色增加到employeeRole表
		List<Role> roles = employee.getRoles();
		for (Role role : roles) {
			employeeRoleMapper.addEmployeeRole(new EmployeeRole(null, employee, role));
		}
	}
	
	/**
	 * 注销用户
	 * @param employeeId
	 */
	public void logOutEmployee(Integer employeeId){//status:0-注销，1-可用，2-暂停
		Employee employee = employeeMapper.fetchEmployeeById(employeeId);
		employee.setStatus(0);
		employeeMapper.updateEmployee(employee);
	}

	@Override
	public int getEmployeeCount(Integer departmentId, String search,String nrsearch) {
		return employeeMapper.getEmployeeCount(departmentId, search,nrsearch);
	}



}
